import common from '../vendor/common'
import '../scss/user_revise_phone1.scss'
import { getCode, oldTel } from '../vendor/api'
try{
    const mui = common.mui
    const Vue = common.Vue
    var tel = null
    var VueTel = null
    var newTel = null //预加载修改手机号码页面
    var sms = null //验证码
    var data = {}
    var time = null //验证码时效
    mui.init({
        swipeBack:true //启用右滑关闭功能
    });
    mui.plusReady(function(){
        VueTel = new Vue({
            el: '#sms',
            data: {
                tel: ''
            }
        })
        // 接收父页面传递来的参数
        window.addEventListener('upTel', function(e){
            // console.log('nihao', e.detail.tel)
            tel = e.detail.tel
            VueTel.tel = e.detail.tel.slice(0,3) + '****' + e.detail.tel.slice(7)
            data = {}
            time = null
            sms = null
            mui('#setSms')[0].value = null
        })
        // 预加载绑定新手机页面
        newTel = mui.preload({
            url: 'user_revise_phone2.html',
            id: 'user_revise_phone2.html',
            styles:{
                popGesture: 'hide'
            }
        })
        // 点击跳转
        mui('.loginOut')[0].addEventListener('tap', function(){
            sms = mui('#setSms')[0].value
            data.tel = tel
            data.smsCode = sms
            // 隐藏键盘
            mui('#setSms')[0].blur()
            // 验证并提交
            if(!sms || sms.trim() == ''){
                mui.alert('请输入验证码')
            } else {
                oldTel(data).then((res) => {
                    console.log(JSON.stringify(res))
                    if(res.data.status){
                        var revise = plus.webview.getWebviewById('user_revise_phone2.html')
                        mui.fire(revise, 'revise')
                        newTel.show('slide-in-right', 300)
                    } else {
                        mui.alert(res.data.message)
                    }
                })
            }
        })
        // 获取验证码
        mui('#getSms')[0].addEventListener('tap',function login(e){
            if(tel){
                // 拼接参数
                var data = tel +'/'+'reset_tel'
                getCode(data).then(function(res){
                    console.log('asfsdgsdgsdf',JSON.stringify(res))
                    if(res.success){
                        time = 120; //定义验证码失效的时间
                        mui('#getSms')[0].classList.add('mui-hidden')
                        mui('#time')[0].classList.remove('mui-hidden')
                        mui('#time')[0].innerHTML = time + 's'
                        var outTime = setInterval(() => {
                            time --;
                            if(time <= 0){
                                mui('#getSms')[0].classList.remove('mui-hidden')
                                mui('#time')[0].classList.add('mui-hidden')
                                mui('#getSms')[0].innerHTML = '重新获取验证码'
                                clearInterval(outTime);
                                return;
                            }
                            mui('#time')[0].innerHTML = time + 's'
                        },1000);
                    }
                })
            } 
        })
    })
}catch(e){
    console.log('user_revise_phone1.html出错了')
}